package com.jdyfy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdyfy.dto.ProjectAnnounceDTO;
import com.jdyfy.entity.*;
import com.jdyfy.result.Result;
import com.jdyfy.service.AnnouncementDepartmentService;
import com.jdyfy.service.AnnouncementService;
import com.jdyfy.service.WinNotiProjectService;
import com.jdyfy.service.impl.WinNotiProjectServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jdyfy")
@Slf4j
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @Resource
    private AnnouncementDepartmentService announcementDepartmentService;

    @Autowired
    private WinNotiProjectService winNotiProjectService;

    //添加或修改
    @PostMapping("/announcement/save")
    public boolean addOrUpdateAnnouncement(@RequestBody Announcement announcement) {
        return announcementService.saveOrUpdate(announcement);
    }

    //根据项目编号集合查询每个项目对应的公告信息，集合
    @PostMapping("/getAnnouncements")
    public List<Announcement> getAnnouncements(@RequestBody List<String> projectNumbers) {
        return announcementService.getAnnouncementsByProjectNumbers(projectNumbers);
    }

    //根据公告名称集合查询公告对应的企业
    @PostMapping("/getEnlistsDepartment")
    public List<AnnouncementDepartment> getEnlistsDepartment(@RequestBody List<String> announcements) {
        return announcementDepartmentService.getEnlistsDepartment(announcements);
    }

    //根据公告名称查询
    @GetMapping("/announcement/findByName")
    public Result<Announcement> getByAnnouncementName(@RequestParam String announcementName){
        Announcement announcement = announcementService.getByAnnouncementName(announcementName);
        return Result.success(announcement);
    }

    //根据公告名称查询报名企业
    @PostMapping("/getEnlistsDepartments")
    public AnnouncementDepartment getEnlistDepartments(@RequestBody String announcementName) {
        return announcementDepartmentService.getEnlistsDepartments(announcementName);
    }

    /**
     * 根据企业名称查询报名公告和项目
     *
     * @param pageNum 当前页码
     * @param pageSize 每页显示的记录数
     * @return 分页后的项目数据
     */
    @GetMapping("/getEnlistsAnnouncementsBydepartment")
    public Result<IPage<ProjectAnnounceDTO>> getEnlistsAnnouncementsBydepartment(@RequestParam Integer pageNum,
                                                                                 @RequestParam Integer pageSize,
                                                                                 @RequestParam String departmentName,
                                                                                 @RequestParam(required = false) String projectName,
                                                                                 @RequestParam(required = false) String projectNumber) {

        // 从数据库获取所有项目数据
        List<ProjectAnnounceDTO> projectAnnounces = announcementDepartmentService.getEnlistsAnnouncementsBydepartment(departmentName,projectName,projectNumber);  // 从数据库查询所有项目，假设此方法已在 Service 中实现

        // 手动进行分页操作
        Page<ProjectAnnounceDTO> page = new Page<>(pageNum, pageSize);  // 创建 Page 对象，设置当前页码和每页显示条数

        // 计算分页的开始和结束位置
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, projectAnnounces.size());  // 确保不越界

        // 获取分页数据
        List<ProjectAnnounceDTO> pageRecords = projectAnnounces.subList(startIndex, endIndex);

        // 设置分页数据
        page.setRecords(pageRecords);
        page.setTotal(projectAnnounces.size());  // 设置总记录数

        // 返回分页结果
        return Result.success(page);
    }

    /**
     * 获取是否已经报名
     * @param announcementName
     * @param departmentName
     * @return
     */
    @GetMapping("/ad/getByadName")
    public Result<Boolean> getAdIsHave(@RequestParam String announcementName, @RequestParam String departmentName) {
        int adIsHave = announcementDepartmentService.getAdIsHave(announcementName, departmentName);
        return Result.success(adIsHave > 0);
    }

    /**
     * 获取企业报名项目数量
     * @param departmentName
     * @return
     */
    @GetMapping("/adCountByDepartment")
    public Result<Long> adCountByDepartment(@RequestParam String departmentName) {
        QueryWrapper<AnnouncementDepartment> ad = new QueryWrapper<>();
        ad.eq("department_name", departmentName);
        Long count = announcementDepartmentService.count(ad);
        return Result.success(count);
    }

    /**
     * 获取企业报名中标数量
     * @param departmentName
     * @return
     */
    @GetMapping("/winnotiProjectCountByDepartment")
    public Result<Long> winnotiProjectCountByDepartment(@RequestParam String departmentName) {
        QueryWrapper<WinNotiProject> wp = new QueryWrapper<>();
        wp.eq("dealer", departmentName);
        Long count = winNotiProjectService.count(wp);
        return Result.success(count);
    }
}
